package com.heo.coder;

import java.util.List;
import org.apache.log4j.Logger;

import com.heo.coder.packet.SocketPacket;
import com.heo.coder.packet.base.BasePacket;
import com.heo.util.ByteHelper;
import com.heo.util.Utils;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class CarDeviceDecoder extends ByteToMessageDecoder {

	private static Logger log = Logger.getLogger(CarDeviceDecoder.class);

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		if (in.readableBytes() < BasePacket.START_MARKER_LENGTH) {
			return;
		}

		in.markReaderIndex();

		byte[] startByte = new byte[BasePacket.START_MARKER_LENGTH];
		in.readBytes(startByte);

		String startCode = ByteHelper.byteToHexStringNoSpace(startByte);

		if (!BasePacket.START_MARKER_LENGTH_CODE.equals(startCode)) {
			ctx.close();
			return;
		}

		if (in.readableBytes() < BasePacket.HEAD_LENGTH) {
			in.resetReaderIndex();
			return;
		}

		if (in.readableBytes() < BasePacket.HEAD_LENGTH) {
			return;
		}
		byte[] headByte = new byte[BasePacket.HEAD_LENGTH];
		in.readBytes(headByte);
		String headCode = ByteHelper.byteToHexStringNoSpace(headByte);
		if (!Utils.checkNull(headCode)) {
			in.resetReaderIndex();
			return;
		}
		String head = headCode.substring(0, 8);
		String headStr = ByteHelper.strReversal(head);
//		String area = headStr.substring(0, 2); // 区
//		String county = headStr.substring(2, 4); // 县
//		String address = headStr.substring(4, 8); // 终端地址
//		String msgBin = ByteHelper.hexString2binaryString(address);
		String msta = headCode.substring(8, 12);
		String sequenceId = headCode.substring(12, 20);
		String id = "" + ByteHelper.hex16ToInt(ByteHelper.strReversal(sequenceId));
		String start = headCode.substring(20, 22);
		String control = headCode.substring(22, 24);
		String controlStr = ByteHelper.hexString2binaryString(control);
		String bodyLengthStr = ByteHelper.strReversal(headCode.substring(24, 28));
		int bodyLength = ByteHelper.hex16ToInt(bodyLengthStr); // 消息体长度 0000 0000 0000 0110 6

		if (in.readableBytes() < bodyLength) {
			in.resetReaderIndex();
			return;
		}

		String msgContent = "";
		if (bodyLength > 0) {
			byte[] contentByte = new byte[bodyLength];
			in.readBytes(contentByte);
			msgContent = ByteHelper.byteToHexStringNoSpace(contentByte);
		}

		if (in.readableBytes() < BasePacket.CRC_MARKER_LENGTH) {
			in.resetReaderIndex();
			return;
		}
		byte[] bytes = new byte[BasePacket.CRC_MARKER_LENGTH]; // 传输正常
		in.readBytes(bytes);
		String checkCode = ByteHelper.byteToHexStringNoSpace(bytes);
		String data = startCode + headStr + msta + sequenceId + start + control + bodyLengthStr + msgContent;
		String check = ByteHelper.makeCheckSum(data);
//		System.out.println("校验码 = " + checkCode + "，校验和=" + check);
		if(!check.equals(checkCode)) {
			return;
		}
		
		if (in.readableBytes() < BasePacket.END_MARKER_LENGTH) {
			in.resetReaderIndex();
			return;
		}
		bytes = new byte[BasePacket.END_MARKER_LENGTH]; // 传输正常
		in.readBytes(bytes);
		String endCode = ByteHelper.byteToHexStringNoSpace(bytes);
		if (!BasePacket.END_MARKER_LENGTH_CODE.equals(endCode)) {
			ctx.close();
			return;
		}
		SocketPacket socketPacket = new SocketPacket(head, sequenceId, bodyLength, msta, control, msgContent, checkCode);
		socketPacket.setMacAddress("00" + id);
	    out.add(socketPacket);
	}
}
